/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myproject.ordendigital.dao.hibernate;

import com.myproject.ordendigital.dao.MesaDao;
import com.myproject.ordendigital.model.Mesa;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrador
 */
@Component
public class MesaDaoHibernate extends HibernateDaoSupport
        implements MesaDao {

    @Autowired
    public MesaDaoHibernate(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Mesa> obtenerListaMesa(Mesa mesa) {
        StringBuffer query = new StringBuffer("from Mesa p where 1 = 1");
        if (mesa.getDescripcion() != null
                && !mesa.getDescripcion().isEmpty()) {
            query.append(" where upper(descripcion) like '%").append(mesa.getDescripcion().toUpperCase()).append("%'");
        }
        List<Mesa> list = getHibernateTemplate().find(query.toString());
        return list;
    }

    @Override
    public Mesa obtenerMesaById(Integer idMesa) {
        Mesa mesa = getHibernateTemplate().get(Mesa.class, idMesa);
        return mesa;
    }

    @Override
    public void guardarMesa(Mesa mesa) {
        getHibernateTemplate().saveOrUpdate(mesa);
    }

    @Override
    public void eliminarMesa(Integer idMesa) {
        Mesa mesa = obtenerMesaById(idMesa);
        getHibernateTemplate().delete(mesa);
    }
}
